package com.blueberry.commbank.domain.service.impl;

import com.blueberry.commbank.domain.entity.Account;
import com.blueberry.commbank.domain.entity.ExchangeRate;
import com.blueberry.commbank.domain.entity.Money;
import com.blueberry.commbank.domain.service.AccountTransferService;
import com.blueberry.commbank.domain.service.ExchangeRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountTransferServiceImpl implements AccountTransferService {
    @Autowired
    private ExchangeRateService exchangeRateService;

    @Override
    public void transfer(Account sourceAccount, Account targetAccount, Money money) {
        ExchangeRate rate = exchangeRateService.getExchangeRate(sourceAccount.getCurrency(),
                targetAccount.getCurrency());
        Money targetMoney = rate.exchange(money);
        sourceAccount.withdraw(money);
        targetAccount.deposit(targetMoney);
    }
}
